package io.renren.service.repair.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;

import io.renren.common.utils.ConvertUtils;
import io.renren.dao.repair.RepairNoticeDao;

import io.renren.dto.repair.RepairNoticeDTO;
import io.renren.entity.repair.RepairNoticeEntity;
import io.renren.service.repair.RepairNoticeService;
import io.renren.vo.RepairNoticeRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 系统公告表
 *
 * @author dio 167762802@QQ.com
 * @since 1.0.0 2025-04-12
 */
@Service
public class RepairNoticeServiceImpl extends CrudServiceImpl<RepairNoticeDao, RepairNoticeEntity, RepairNoticeDTO> implements RepairNoticeService {

    @Resource
    private RepairNoticeDao repairNoticeDao;

    @Override
    public QueryWrapper<RepairNoticeEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<RepairNoticeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<RepairNoticeRespVO> getRepairList() {

        List<RepairNoticeEntity> repairNoticeEntities = repairNoticeDao.selectList(null);
        List<RepairNoticeRespVO> repairNoticeRespVOS = repairNoticeEntities.stream().map(item -> {
            RepairNoticeRespVO repairNoticeRespVO = new RepairNoticeRespVO();
            BeanUtil.copyProperties(item, repairNoticeRespVO);
            return repairNoticeRespVO;
        }).toList();

        return repairNoticeRespVOS;

    }

    @Override
    public RepairNoticeRespVO getDetail(Long id) {
        RepairNoticeEntity repairNoticeEntity = new RepairNoticeEntity();
        repairNoticeEntity=repairNoticeDao.selectById(id);
        return ConvertUtils.sourceToTarget(repairNoticeEntity, RepairNoticeRespVO.class);
    }

    @Override
    public List<RepairNoticeRespVO> getByTitle(String title) {
        // 创建 QueryWrapper，用于构建查询条件
        QueryWrapper<RepairNoticeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title); // 使用 like 方法进行模糊查询

        // 查询符合条件的 RepairNoticeEntity 列表
        List<RepairNoticeEntity> repairNoticeEntities = repairNoticeDao.selectList(queryWrapper);

        // 将 RepairNoticeEntity 列表转换为 RepairNoticeRespVO 列表
        return repairNoticeEntities.stream()
                .map(entity -> ConvertUtils.sourceToTarget(entity, RepairNoticeRespVO.class))
                .collect(Collectors.toList());
    }

}